/**
 * 
 */
package com.trendytech.tds.system.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.trendytech.tds.entity.Role;
import com.trendytech.tds.entity.User;
import com.trendytech.tds.system.dao.IRoleDAO;

import hm.base.dao.impl.AbstractDAO;

/**
 * @author Robin
 *
 */
@Repository
public class RoleDAOImpl extends AbstractDAO<Role> implements IRoleDAO {

	private static final String MAPPER_SELECT_BY_RESOURCE = "selectByResourceId";
	private static final String MAPPER_SELECT_BY_USER = "selectByUserId";
	private static final String MAPPER_INSERT_ROLE_RESOURCE = "insertRoleResource";
	private static final String MAPPER_SELECT_BY_ROLE = "selectByRoleId";
	private static final String MAPPER_SELECT_BY_ROLEID_USER = "selectByRoleIdForUser";
	private static final String MAPPER_DELETE_ROLE_RESOURCE = "deleteRoleResource";
	private static final String MAPPER_DELETE_USERROLE_BY_ROLEID = "deleteUsersByRoleID";
	private static final String MAPPER_DELETE_RESOURCEROLE_BY_ROLEID = "deleteRoleResourceByRoleId";
	private static final String MAPPER_SELECT_RESOURCEROLE_BY_ROLEID = "getRoleResourceByRoleId";
	private static final String MAPPER_SELECT_RESOURCEROLE_COUNT = "countRoleResource";
	private static final String MAPPER_SELECT_BY_NAME = "selectByName";
	@Override
	public List<Role> getByResourceId(String resourceId) {
		return sqlSession.selectList(getMapperId(MAPPER_SELECT_BY_RESOURCE), resourceId);
	}

	@Override
	public List<Role> getByUserId(String userId) {
		return sqlSession.selectList(getMapperId(MAPPER_SELECT_BY_USER), userId);
	}
	
	@Override
	public List<User> getByRoleIdForUser(String roleId) {
		return sqlSession.selectList(getMapperId(MAPPER_SELECT_BY_ROLEID_USER), roleId);
	}

	@Override
	public Role getByRoleId(String roleId) {
		List<Role> roles = sqlSession.selectList(
				getMapperId(MAPPER_SELECT_BY_ROLE), roleId);
		if (roles != null && !roles.isEmpty()) {
			return roles.get(0);
		}
		return null;
	}
	
	@Override
	public int insertRoleResource(String roleId, String resourceId) {
		Map<String, String> roleResMap = new HashMap<String, String>();
		roleResMap.put("roleId", roleId);
		roleResMap.put("resourceId", resourceId);
		return sqlSession.insert(getMapperId(MAPPER_INSERT_ROLE_RESOURCE), roleResMap);
	}
	
	@Override
	public int deleteRoleResource(String roleId, String resourceId){
		Map<String, String> roleResMap = new HashMap<String, String>();
		roleResMap.put("roleId", roleId);	
		roleResMap.put("resourceId", resourceId);
		return sqlSession.delete(getMapperId(MAPPER_DELETE_ROLE_RESOURCE), roleResMap);
	}
	
	@Override
	public int deleteUsersRoleByRoleID(String roleId){
		Map<String, String> roleUserMap = new HashMap<String, String>();
		roleUserMap.put("roleId", roleId);	
		return sqlSession.delete(getMapperId(MAPPER_DELETE_USERROLE_BY_ROLEID), roleUserMap);
	}
	
	@Override
	public int deleteResourcesRoleByRoleID(String roleId){
		Map<String, String> roleUserMap = new HashMap<String, String>();
		roleUserMap.put("roleId", roleId);	
		return sqlSession.delete(getMapperId(MAPPER_DELETE_RESOURCEROLE_BY_ROLEID), roleUserMap);
	}
	
	@Override
	public List<String> getResourcesRoleByRoleID(String roleId){
		Map<String, String> roleUserMap = new HashMap<String, String>();
		roleUserMap.put("roleId", roleId);	
		return sqlSession.selectList(getMapperId(MAPPER_SELECT_RESOURCEROLE_BY_ROLEID), roleUserMap);
	}

	@Override
	public int checkRoleResource(String roleId, String resourceId) {
		Map<String, String> roleResourceMap = new HashMap<String, String>();
		roleResourceMap.put("roleId", roleId);
		roleResourceMap.put("resourceId", resourceId);
		return sqlSession.selectOne(getMapperId(MAPPER_SELECT_RESOURCEROLE_COUNT), roleResourceMap);

	}

	@Override
	public Role getByName(String name) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		return sqlSession.selectOne(getMapperId(MAPPER_SELECT_BY_NAME),map);
	}

}
